import connect
import random as r
from sqlalchemy import create_engine, or_
from sqlalchemy.orm import scoped_session, sessionmaker, session
import createtables
import random as r
import choicedemo

from SQLAlchemy import constants

IP=constants.IP
# db_connect_string='mysql://liu:liu@121.41.203.238:3306/bishe?charset=utf8'
db_connect_string='mysql://liu:liu@'+IP+':3306/bishe?charset=utf8'
engine=create_engine(db_connect_string)
SessionType=scoped_session(sessionmaker(bind=engine,expire_on_commit=False))
def GetSession():
    return SessionType()

from contextlib import contextmanager
@contextmanager
def session_scope():
    session=GetSession()
    try:
        yield session
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()

class Message():
    def  admin():
        for i in range(1,4):
            name=choicedemo.Choice_name()
            gender=choicedemo.Choice_gender()
            telephone=choicedemo.Choice_telephone()
            connect.Insert().Admin(i,name,gender,telephone)
    def  course():
        for i in range(1001,1009):
            course=['Android','嵌入式','计算机组成','基层管理','python','创业培训','投资学','心理学']
            print(i,i-1001,course[i-1001])
            connect.Insert().Course(i,course[i-1001])
    def  time_table():
        k=2001
        for i in range(1,6):
            for j in range(1,5):
                connect.Insert().TimeTable(k,'周'+str(i),'第'+str(j)+'节')
                k=k+1
    def  clas():
        for i in range(3001,3013):
            connect.Insert().Class(i,'计算机科学与技术','2017',i-3000)
    def  classroom():
        for i in range(4001,4021):
            connect.Insert().Classroom(i,i-3900)
    def  teacher():
        k=10001
        for i in range(4):
            for j in range(4):
                name=choicedemo.Choice_name()
                gender=choicedemo.Choice_gender()
                telephone=choicedemo.Choice_telephone()
                connect.Insert().Teacher(k,name,gender,telephone,1001+i)
                k=k+1
        for i in range(4):
            name=choicedemo.Choice_name()
            gender=choicedemo.Choice_gender()
            telephone=choicedemo.Choice_telephone()
            connect.Insert().Teacher(k, name, gender, telephone, 1005 + i)
            k=k+1
    def student():
        k = 20001
        for i in range(12):
            for j in range (20):
                name = choicedemo.Choice_name()
                gender = choicedemo.Choice_gender()
                telephone = choicedemo.Choice_telephone()
                connect.Insert().Student(k, name, gender, telephone, 3001+i)
                k = k + 1
    def  choise_course():
        teacher_id=10017
        student_id= 20001
        for i in range(4):
            for j in range(60):
                connect.Insert().ChoiseCourse(teacher_id+i,j+i*60+student_id)
    def  mark():
        k = 20001
        m=1001
        mark_id=8001
        for i in range(240):
            for j in range(5):
                if j==4:
                    connect.Insert().Mark(mark_id+j+i*5,k+i,m+j+r.randint(0,3),r.randint(60,100))
                else:
                    connect.Insert().Mark(mark_id+j+i*5,k+i,m+j,r.randint(60,100))
    def  school_time_table():
        k = 2001
        m=4001
        class_id=3001
        teacher_id = 10001
        x=5001
        for i in range(5):
            for j in range(4):
                for l in range (3):
                    connect.Insert().SchoolTimeTable(x,k+i, m+j, class_id+l+j*3, teacher_id+j+i*4)
                    x=x+1
    def  class_teacher():
        k = 2001
        m = 4001
        class_id = 3001
        teacher_id = 10001
        x = 5001
        for i in range(4):
            for j in range(4):
                for l in range(3):
                    connect.Insert().ClassTeacher(teacher_id + j + i * 4,class_id + l+j*3)
    def  assess():
        teacher_id = 10001
        student_id=20001
        assess_id=6001
        for i in range(4):
            for j in range(4):
                for k in range(60):
                    s=choicedemo.Choice_Address()
                    connect.Insert().Assess(assess_id,teacher_id+i*4+j,student_id+k+j*60,s)
                    assess_id=assess_id+1

        for i in range(4):
            for j in range(60):
                s=choicedemo.Choice_Address()
                connect.Insert().Assess(assess_id, teacher_id + i + 16, student_id + i*60 + j , s)
                assess_id = assess_id + 1
    def  design():
        teacher_id = 10001
        student_id=20001
        design_id=7001
        for i in range(20):
            for j in range(12):
                connect.Insert().Design(design_id=design_id+j+i*12,teacher_id=teacher_id+i,student_id=student_id+j+i*12,name=choicedemo.Choice_Assess())
    def  log_in():
        with session_scope() as session:
            admin=session.query(createtables.Admin).filter().all()
        for i in admin:
            connect.Insert().LogIn(i.admin_id)
        with session_scope() as session:
            student=session.query(createtables.Student).filter().all()
        for i in student:
            connect.Insert().LogIn(i.student_id)
        with session_scope() as session:
            teacher=session.query(createtables.Teacher).filter().all()
        for i in teacher:
            connect.Insert().LogIn(i.teacher_id)


Message.admin()
Message.course()
Message.time_table()
Message.clas()
Message.classroom()
Message.teacher()
Message.student()
Message.choise_course()
Message.mark()
Message.school_time_table()
Message.class_teacher()
Message.assess()
Message.design()
Message.log_in()
